package com.frame.base.utils;

import android.content.Context;
import android.widget.PopupWindow;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.bigkoo.svprogresshud.SVProgressHUD;

/**
 * Created by wz on 2016/8/11.
 * 弹窗工具
 */
public class DialogUtil {

    private DialogUtil() {
    }

    private static AlertView alertView;

    private static MyDissMissListener dissMissListener = new MyDissMissListener();

    private static boolean isAlertViewshowing;
    public static SVProgressHUD svProgressHUD;

    /**
     * 显示actionsheet
     *
     * @param title
     * @param options
     * @param context
     * @param listener
     */
    public static void showActionSheet(String title, String[] options, Context context, OnItemClickListener listener) {
        alertView = new AlertView(title, null, "取消", null, options, context, AlertView.Style.ActionSheet, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(true).show();
        isAlertViewshowing = true;
    }

    public static void showActionSheet(String title, String content, String[] options, Context context, OnItemClickListener listener) {
        alertView = new AlertView(title, content, "取消", null, options, context, AlertView.Style.ActionSheet, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(true).show();
        isAlertViewshowing = true;
    }

    public static void showDiaLog(String title, String msg, String[] options, Context context, OnItemClickListener listener) {
        showDiaLog(title, msg, options, context, true, listener);
    }

    public static void showDiaLog(String title, String msg, String[] options, Context context, boolean cancelable, OnItemClickListener listener) {
        alertView = new AlertView(title, msg, null, null, options, context, AlertView.Style.Alert, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(cancelable).show();
        isAlertViewshowing = true;
    }

    public static PopupWindow popupWindow;

//    public static void showPopupWindow(Context context, View view, String title, String message, String[] contents, String strSure, String strCancle,
//                                       View.OnClickListener CommitClicklistener, View.OnClickListener CancleClicklistener) {
//
//        View inflate = View.inflate(context, R.layout.layout_alret_dialog, null);
//        popupWindow = new PopupWindow(inflate, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
////        popupWindow.getWindow().setLayout(CustomAlertDialogUtil.getScreenWidth(context), LinearLayout.LayoutParams.WRAP_CONTENT);
//        NonNullText tvTitle = (NonNullText) inflate.findViewById(R.id.tv_title);
//        NonNullText tvMessage = (NonNullText) inflate.findViewById(R.id.tv_message);
//        NonNullText btn_commit = (NonNullText) inflate.findViewById(R.id.btn_commit);
//        NonNullText btn_cancel = (NonNullText) inflate.findViewById(R.id.btn_cancel);
//        LinearLayout llContent = (LinearLayout) inflate.findViewById(R.id.ll_content);
//        for (int i = 0; i < contents.length; i++) {
//            TextView tvContent = new TextView(context);
//            tvContent.setText(contents[i]);
//            llContent.addView(tvContent);
//        }
//        tvTitle.setText(title);
//        tvMessage.setText(message);
//        btn_commit.setText(strSure);
//        btn_cancel.setText(strCancle);
//
//        inflate.findViewById(R.id.ll_window_bg_bg).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popupWindow.dismiss();
//            }
//        });
//        btn_commit.setOnClickListener(CommitClicklistener);
//        btn_cancel.setOnClickListener(CancleClicklistener);
//        popupWindow.setTouchable(true);
//        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
//
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return false;
//                // 这里如果返回true的话，touch事件将被拦截
//                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
//            }
//        });
//
//        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
//        // 这里是API的一个bug
//        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
//                R.drawable.shape_popupwindow_defaut));
//        popupWindow.setOutsideTouchable(false);
//        // 设置好参数之后再show
//        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//
//    }

    public static void dismissPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }

    }

    public interface OnCustomButtonClickListener {
        void OnCustomButtonClicked(int position);
    }

    public static OnCustomButtonClickListener click;

    public static void setPopupWindowClick(OnCustomButtonClickListener clickListener) {
        click = clickListener;
    }

    /**
     * 弹窗消失
     */
    public static void dissMiss() {
        if (alertView != null) {
            alertView.dismiss();
        }
    }

    public static boolean isShowingAlertView() {
        return isAlertViewshowing;
    }

    static class MyDissMissListener implements OnDismissListener {
        @Override
        public void onDismiss(Object o) {
            isAlertViewshowing = false;
        }
    }

    public boolean getProgressStatus() {
        return svProgressHUD.isShowing();
    }

    protected void showLabel() {
        svProgressHUD.show();
    }

    public static void showLoadingLabel(Context context) {
        showLoadingLabel(context, "加载中...");
    }

    public static void showLoadingLabel(Context context, String msg) {
        svProgressHUD = new SVProgressHUD(context);
        svProgressHUD.showWithStatus(msg);
    }

    public static void dismissLoadingLabel() {
        if (svProgressHUD.isShowing()) {
            svProgressHUD.dismiss();
        }
    }
}
